package com.timedomain.him.remake.utils;

import java.io.*;

public class PhysicalFileUtils {

    public static String readAllText(File path){
        try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)))){
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = br.readLine())!=null){
                sb.append(line);
                sb.append(System.lineSeparator());
            }
            return sb.toString();
        }catch (Exception ex){
            ex.printStackTrace();
            return null;
        }
    }
    public static String readAllText(InputStream s){
        try(BufferedReader br = new BufferedReader(new InputStreamReader(s))){
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = br.readLine())!=null){
                sb.append(line);
                sb.append(System.lineSeparator());
            }
            return sb.toString();
        }catch (Exception ex){
            ex.printStackTrace();
            return null;
        }
    }

    public static void deleteDirectory(File file){
        if(file.exists()&&file.isDirectory()){
            File[] files = file.listFiles();
            for(File f:files){
                if(f.isFile()){
                    f.delete();
                }else {
                    deleteDirectory(f);
                }
                f.delete();
            }
            file.delete();
        }
    }
}
